%this function calculates the RMS value of a set of already stored fields.

%PathFiles: cell vector with the path of the files to use
%MeanField: three dimensional array containing the mean value of the fields
%whose RMS value is being calculated

%created by Ricardo Mejia-Alvarez
%University of Illinois at Urbana-Champaign
%Urbana, IL. 09/20/2010

function [RMSField] = RMScalculator(PathFiles,MeanField)

L = size( MeanField );
N = length(PathFiles);
PathFiles = char(PathFiles);

for k = 1 : N
    path = PathFiles(k , :);
    
    [~,I,J,~,~,Ucell] = matrix_Cell(path);
    
    U = zeros( J , I , L(3) );
    
    for kk = 1 : L(3)
        U(: , : , kk) = Ucell{kk};
    end
    
    if k == 1
        RMSField = zeros(J , I , L(3));
    end
    
    RMSField = RMSField + (U - MeanField).^2;
    
end

RMSField = sqrt( RMSField * N^(-1) );

end